package cn.com.dbappsecurity.service.impl;

import java.util.List;

import cn.com.dbappsecurity.dao.DemoMapper;
import cn.com.dbappsecurity.entity.Demo;
import cn.com.dbappsecurity.frame.Constant;
import cn.com.dbappsecurity.frame.annotations.Transactional;
import cn.com.dbappsecurity.frame.exception.FrameException;
import cn.com.dbappsecurity.service.IDemo;

public class DemoImpl extends BaseServiceImpl implements IDemo{

	/*
	 * 获取Dao实例
	 */
	private DemoMapper demoMapper = getMapper(DemoMapper.class);
	
	@Transactional//标识自动处理事务
	@Override
	public boolean insert(String name, int age) throws Exception {
		/**
		 * 响应错误信息到前台可直接抛出FrameException异常，会要求传入错误代码，在FrameException常量中定义，
		 * 框架会根据错误代码从资源文件/locale/language_xxx.properties中读取对应错误信息，因为这里设计到国际化处理
		 */
		if(name == null || age < 1){
			throw new FrameException(FrameException.TYPE_ARGU_ERROR);
		}
		
		Demo demo = new Demo();
		demo.setName(name);
		demo.setAge(26);
		
		int num = demoMapper.insert(demo);
		return num == 1;
	}

	@Transactional
	@Override
	public boolean delete(int id) throws Exception {
		if(id < 1){
			throw new FrameException(FrameException.TYPE_ARGU_ERROR);
		}
		int num = demoMapper.delete(id);
		return num == 1;
	}
	
	@Override
	public List<Demo> search(String name,int age,int page,int rows) throws Exception {
		if(rows > Constant.MAX_PAGE_AVG){
			throw new FrameException(FrameException.TYPE_NUM_EXCEED_MAX);
		}
		List<Demo> demos = demoMapper.search(name, age, getLimit(page,rows), rows);
		return demos;
	}

	@Override
	public int searchCount(String name, int age) throws Exception {
		return demoMapper.searchCount(name, age);
	}

}
